package com.baosight.risk.web.system;

import com.baosight.risk.api.system.ISysCoderuleService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.system.entity.SysCoderule;
import com.baosight.risk.service.system.vo.SysCoderuleVO;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类 名: SysCoderuleController
 * 描 述: 编码流水号管理
 * 作 者: 黎秋荣
 * 创 建：2019年04月15日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@Controller
@RequestMapping("/system/coderule")
public class SysCoderuleController extends BaseController {
    private String prefix = "system/coderule/";

    @Autowired
    private ISysCoderuleService coderuleService;

    /**
     * 返回用户管理页面
     *
     * @return
     */
    @GetMapping()
    public String coderule() {
        return prefix + "coderule";
    }

    /**
     * 加载添加页面
     *
     * @return
     */
    @GetMapping("add")
    public String add() {
        return prefix + "add";
    }

    /**
     * 加载添加规则页面
     *
     * @return
     */
    @GetMapping("addrule")
    public String addrule(SysCoderuleVO vo, ModelMap mmp) {
        mmp.put("coderule", vo);
        return prefix + "addrule";
    }

    /**
     * 规则添加
     *
     * @param coderule
     * @return
     */
    @PostMapping("save")
    @Log(title = "规则添加")
    @ResponseBody
    public AjaxResult save(SysCoderule coderule) {
        coderule = (SysCoderule) BeanUtils.initBean(coderule);
        coderule.setDeleteMark(10);
        log.info("规则添加：" + coderule.toString());
        //todo：新增的时候如果有流水号需要新增redis
        return toAjax(coderuleService.insert(coderule));
    }

    /**
     * 根据ID查询跳转修改页面
     *
     * @param id
     * @param mmp
     * @return
     */
    @GetMapping("edit")
    public String edit(@RequestParam("id") String id, ModelMap mmp) {
        mmp.put("coderule", coderuleService.get(id));
        return prefix + "edit";
    }

    /***
     * 修改编码规则
     * @param coderule
     * @return
     */
    @PostMapping("update")
    @Log(title = "编码规则修改", businessType = BusinessType.UPDATE)
    @ResponseBody
    @RequiresPermissions("system:coderule:update")
    public AjaxResult update(SysCoderule coderule) {
        log.info("编码规则修改：" + coderule.toString());
        //todo：如果修改的时候修改了流水号的规则，需要修改redis
        return toAjax(coderuleService.update(coderule));
    }

    /**
     * 分页查询编码规则
     *
     * @param coderule
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public AjaxResult select(SysCoderule coderule) {
        log.info("编码规则查询参数：" + coderule.toString());
        startPage();
        return getResultPage(coderuleService.selectCoderule(coderule));
    }

    /**
     * 规则数据判断
     *
     * @param coderule
     * @return
     */
    @PostMapping("judge")
    @ResponseBody
    public Boolean judge(SysCoderule coderule) {
        log.info("编码规则判断是否重复参数：" + coderule.toString());
        Boolean flag = true;
        if (CollectionUtils.isEmpty(coderuleService.selectCoderule(coderule))) {
            flag = false;
        }
        return flag;
    }

    /**
     * 分页查询编码规则
     *
     * @param enCode 对象编号
     * @return
     */
    @PostMapping("generate")
    @ResponseBody
    public String select(String enCode) {
        log.info("生成编码参数：" + enCode);
        coderuleService.generateCodeById(enCode);
        return prefix + "coderule";
    }

    /**
     * 批量逻辑删除编码规则
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @Log(title = "规则删除", businessType = BusinessType.DELETE)
    @ResponseBody
    @RequiresPermissions("system:coderule:delete")
    public AjaxResult delete(String ids) {
        List<String> idss = JSONArray.fromObject(ids);
        List<String> coderuleList = coderuleService.getSysCoderuleByIds(idss);
        if (idss.size() != coderuleList.size()) {
            log.error("数据有误！");
        }
        return toAjax(coderuleService.deleteByIds(idss));
    }

}
